<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
	<title>使用Symbol保护对象不想被遍历的属性</title>
	<style type="text/css">
	* {
		padding: 0;
		margin: 0;
	}
    </style>
</head>
<body>
	<div class="container">
		<h1>使用Symbol保护对象不想被遍历的属性</h1>
	</div>
	<script src="https://cdn.staticfile.org/jquery/3.2.0/jquery.js"></script>
	<script type="text/javascript">
	// 如果对象属性不想被遍历，可以使用Symbol保护
	 const site = Symbol('网站名称')
	 class User {
	 	constructor(name) {
	 		this.name = name ;
	 		this[site] = '后盾人'
	 	}

	 	getName() {
	 		return `${this[site]}-${this.name}`;
	 	}
	 }

	 const hd = new User('向军')
	 console.log(hd.getName())

	 for(const key in hd) {
	 	console.log(key, hd[key])
	 }
	 console.log('---------------------------------------------------')
	 
	 for(const key of Object.getOwnPropertySymbols(hd)) {
	 	console.log(key, typeof key, type(key), hd[key])
	 }

	 console.log('---------------------------------------------------')

	 var objectSymbols = Object.getOwnPropertySymbols(hd);
	 console.log(objectSymbols, typeof objectSymbols, type(objectSymbols))


	 console.log('---------------------Reflect.ownKeys------------------------------')

	 for(const key of Reflect.ownKeys(hd)) {
	 	console.log(key, typeof key, type(key), hd[key])
	 }

	function type(data) {
		return Object.prototype.toString.call(data)
	}
	</script>
</body>
</html>